<template>
  <div class="result-display">
    <h2 class="section-title">🎯 识别结果</h2>

    <div class="stats-cards">
    <div class="stat-card">
      <div class="stat-number">{{ totalDetections }}</div>
      <div class="stat-label">检测到的{{ resultType === 'vehicle' ? '车辆' : '车牌' }}数量</div>
    </div>
    <div class="stat-card">
      <div class="stat-number">{{ Math.round(averageConfidence * 100) }}%</div>
      <div class="stat-label">平均置信度</div>
    </div>
  </div>

    <!-- 车辆检测结果 -->
    <div v-if="resultType === 'vehicle'" class="detection-results">
      <h3 class="subsection-title">🚘 车辆检测结果</h3>
      <div class="results-table">
        <table>
          <thead>
          <tr>
            <th>车辆ID</th>
            <th>位置</th>
            <th>置信度</th>
            <th>尺寸</th>
          </tr>
          </thead>
          <tbody>
          <tr v-for="(vehicle, index) in result.results" :key="index">
            <td>#{{ index + 1 }}</td>
            <td>({{ vehicle.bbox[0] }}, {{ vehicle.bbox[1] }}) - ({{ vehicle.bbox[2] }}, {{ vehicle.bbox[3] }})</td>
            <td>{{ Math.round(vehicle.confidence * 100) }}%</td>
            <td>{{ vehicle.width }} × {{ vehicle.height }} px</td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 车牌识别结果 -->
    <div v-if="resultType === 'plate'" class="recognition-results">
      <h3 class="subsection-title">🔢 车牌识别结果</h3>
      <div class="plates-list">
        <div
            v-for="(plate, index) in result.results"
            :key="index"
            class="plate-card"
        >
          <div class="plate-header">
            <span class="plate-number">{{ plate.plate_number }}</span>
            <span class="plate-type">{{ plate.plate_type }}</span>
          </div>

          <div class="plate-details">
            <div class="detail-item">
              <span class="detail-label">车牌颜色:</span>
              <span class="detail-value">{{ plate.plate_color }}</span>
            </div>

            <div class="detail-item">
              <span class="detail-label">检测置信度:</span>
              <span class="detail-value">{{ Math.round(plate.confidence * 100) }}%</span>
            </div>

            <div class="detail-item">
              <span class="detail-label">位置:</span>
              <span class="detail-value">
                ({{ plate.bbox[0] }}, {{ plate.bbox[1] }}) - ({{ plate.bbox[2] }}, {{ plate.bbox[3] }})
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 结果图片 -->
    <div class="result-image-section">
      <h3 class="subsection-title">📸 标注结果</h3>
      <div class="image-container">
        <img
            v-if="resultImageUrl"
            :src="resultImageUrl"
            :alt="resultType === 'vehicle' ? '车辆检测结果' : '车牌识别结果'"
            class="result-image"
        >
      </div>
    </div>
  </div>
</template>

<script>
import { getPlateColorCode } from '@/utils/helpers'

export default {
  props: {
    result: Object,
    resultType: String // 'vehicle' 或 'plate'
  },
  computed: {
    // 新增计算属性：检测总数
    totalDetections() {
      if (this.resultType === 'vehicle') {
        return this.result.total_vehicles || 0;
      } else {
        return this.result.total_plates || 0;
      }
    },

    // 修改平均置信度计算
    averageConfidence() {
      if (!this.result || !this.result.results?.length) return 0;
      const total = this.result.results.reduce((sum, item) => sum + item.confidence, 0);
      return total / this.result.results.length;
    },

    // 修改结果图片URL处理
    resultImageUrl() {
      if (!this.result) return null;

      // 处理车辆检测的base64图片
      if (this.result.result_image_base64) {
        return `data:image/png;base64,${this.result.result_image_base64}`;
      }

      // 处理车牌识别的图片路径
      if (this.result.result_image_path) {
        return `/static/${this.result.result_image_path.split('/').pop()}`;
      }

      return null
    }
  },
  methods: {
    getPlateColorCode
  },
  mounted() {
    console.log("Received result:", this.result);
    console.log("Result type:", this.resultType);
  },
  watch: {
    resultImageUrl(url) {
      console.log("Image URL:", url);
    }
  }
}
</script>

<style scoped>
.result-display {
  margin-top: 40px;
  padding-top: 40px;
  border-top: 2px solid #eee;
}

.section-title {
  color: #2c3e50;
  font-size: 1.8rem;
  margin: 0 0 30px 0;
  font-weight: 700;
  text-align: center;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 30px;
  margin-bottom: 40px;
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
}

.stat-card {
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  padding: 25px;
  border-radius: 12px;
  text-align: center;
  border: 1px solid #dee2e6;
}

.stat-number {
  font-size: 2.5rem;
  font-weight: 700;
  color: #3498db;
  margin-bottom: 5px;
}

.stat-label {
  color: #7f8c8d;
  font-size: 0.9rem;
}

.subsection-title {
  color: #2c3e50;
  font-size: 1.3rem;
  margin: 0 0 20px 0;
  font-weight: 600;
}

.detection-results,
.recognition-results {
  margin-bottom: 40px;
}

.results-table {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

th, td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

th {
  background-color: #f8f9fa;
  font-weight: 600;
}

tr:hover {
  background-color: #f5f7fa;
}

.plates-list {
  display: grid;
  gap: 20px;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}

.plate-card {
  background: white;
  border: 2px solid #eee;
  border-radius: 12px;
  padding: 25px;
  transition: all 0.3s ease;
}

.plate-card:hover {
  border-color: #3498db;
  box-shadow: 0 8px 20px rgba(0,0,0,0.1);
}

.plate-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.plate-number {
  font-size: 1.5rem;
  font-weight: 700;
  color: #2c3e50;
  font-family: 'Courier New', monospace;
  background: #f8f9fa;
  padding: 8px 16px;
  border-radius: 6px;
  border: 2px solid #dee2e6;
}

.plate-type {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 600;
  background: #d1ecf1;
  color: #0c5460;
}

.plate-details {
  display: grid;
  gap: 15px;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.detail-label {
  color: #7f8c8d;
  font-weight: 500;
}

.detail-value {
  font-weight: 600;
  color: #2c3e50;
}

.result-image-section {
  margin-top: 40px;
}

.image-container {
  text-align: center;
  background: #f8f9fa;
  border-radius: 12px;
  padding: 20px;
}

.result-image {
  max-width: 50%;
  height: auto;
  border-radius: 8px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
</style>